package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.MainDataGroupComprehensiveCostDTO;
import com.ctshk.rpc.system.entity.MainDataGroupComprehensiveCost;
import com.ctshk.rpc.system.mapper.MainDataGroupComprehensiveCostMapper;
import com.ctshk.rpc.system.req.MainDataGroupComprehensiveCostAddReq;
import com.ctshk.rpc.system.req.MainDataGroupComprehensiveCostIdReq;
import com.ctshk.rpc.system.req.MainDataGroupComprehensiveCostListReq;
import com.ctshk.rpc.system.req.MainDataGroupComprehensiveCostUpdateReq;
import com.ctshk.rpc.system.service.IMainDataGroupComprehensiveCostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 团综合成本项 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-11
 */
@DubboService
public class MainDataGroupComprehensiveCostServiceImpl extends ServiceImpl<MainDataGroupComprehensiveCostMapper, MainDataGroupComprehensiveCost> implements IMainDataGroupComprehensiveCostService {

    @Override
    public Result add(MainDataGroupComprehensiveCostAddReq req, TokenUser tokenUser) {
        LocalDateTime now = LocalDateTime.now();
        long id = IdWorker.getId();
        MainDataGroupComprehensiveCost mainDataGroupComprehensiveCost = EntityUtil.copy(req, MainDataGroupComprehensiveCost.class);
        mainDataGroupComprehensiveCost.setId(id);
        mainDataGroupComprehensiveCost.setGmtCreate(now);
        mainDataGroupComprehensiveCost.setGmtModified(now);
        mainDataGroupComprehensiveCost.setCreateId(tokenUser.getId());
        mainDataGroupComprehensiveCost.setModifiedId(tokenUser.getId());
        mainDataGroupComprehensiveCost.setModifiedName(tokenUser.getUsername());
        mainDataGroupComprehensiveCost.setIsDeleted(IsDeletedCode.NO.getCode());
        baseMapper.insert(mainDataGroupComprehensiveCost);
        return Result.success(id);
    }

    @Override
    public MainDataGroupComprehensiveCostDTO query(MainDataGroupComprehensiveCostIdReq req) {
        MainDataGroupComprehensiveCost mainDataGroupComprehensiveCost = baseMapper.selectById(req.getId());
        MainDataGroupComprehensiveCostDTO mainDataGroupComprehensiveCostDTO = EntityUtil.copy(mainDataGroupComprehensiveCost, MainDataGroupComprehensiveCostDTO.class);
        return mainDataGroupComprehensiveCostDTO;
    }

    @Override
    public Result update(MainDataGroupComprehensiveCostUpdateReq req, TokenUser tokenUser) {
        LocalDateTime now = LocalDateTime.now();
        MainDataGroupComprehensiveCost mainDataGroupComprehensiveCost = EntityUtil.copy(req, MainDataGroupComprehensiveCost.class);
        mainDataGroupComprehensiveCost.setModifiedName(tokenUser.getUsername());
        mainDataGroupComprehensiveCost.setModifiedId(tokenUser.getId());
        mainDataGroupComprehensiveCost.setGmtModified(now);
        baseMapper.updateById(mainDataGroupComprehensiveCost);
        return Result.success(req.getId());
    }

    @Override
    public PageResponse<MainDataGroupComprehensiveCostDTO> List(MainDataGroupComprehensiveCostListReq req) {
        Page<MainDataGroupComprehensiveCost> page = new Page<>(req.getPageNo(), req.getPageSize());
        QueryWrapper<MainDataGroupComprehensiveCost> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MainDataGroupComprehensiveCost::getIsDeleted,IsDeletedCode.NO.getCode());
        queryWrapper.lambda().like(StringUtils.isNotBlank(req.getName()),MainDataGroupComprehensiveCost::getTypeName,req.getName());
        queryWrapper.lambda().eq(null != req.getStatus(),MainDataGroupComprehensiveCost::getStatus,req.getStatus());
        queryWrapper.lambda().orderByDesc(MainDataGroupComprehensiveCost::getGmtModified);
        Page<MainDataGroupComprehensiveCost> iPage = baseMapper.selectPage(page, queryWrapper);
        List<MainDataGroupComprehensiveCost> records = iPage.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return new PageResponse<>(new ArrayList<>(), iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
        }
        List<MainDataGroupComprehensiveCostDTO> comprehensiveCostDTOList = EntityUtil.copyList(records, MainDataGroupComprehensiveCostDTO.class);
        return new PageResponse<>(comprehensiveCostDTOList, iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
    }
}
